package coco.blog
{
	import coco.util.WindowUtil;
	
	import cocoBlog.Blog;
	import cocoBlog.data.BlogCount;
	import cocoBlog.data.BlogUser;
	import cocoBlog.event.BlogErrorEvent;
	import cocoBlog.event.BlogResultEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	public class UserBlog extends Blog
	{
		private static var instance:UserBlog;
		

		public static function getInstance():UserBlog
		{
			if(!instance)
			{
				instance = new UserBlog();
			}
			
			return instance;
		}
		
		
		public function UserBlog():void
		{
			initHandlers();
		}
		
		
		private function initHandlers():void
		{
			// Result
			addEventListener(BlogResultEvent.CREATE_FAVORITES_RESULT, resultHandler);
			addEventListener(BlogResultEvent.DELETE_FAVORITES_RESULT, resultHandler);
			addEventListener(BlogResultEvent.SEND_DIRECT_MESSAGE_RESULT, resultHandler);
			addEventListener(BlogResultEvent.DELETE_DIRECT_MESSAGE_RESULT, resultHandler);
			addEventListener(BlogResultEvent.USER_VERIFIER_RESULT, resultHandler);
			addEventListener(BlogResultEvent.REPLY_COMMENT_RESULT, resultHandler);
			addEventListener(BlogResultEvent.COMMENT_STATUES_RESULT, resultHandler);
			addEventListener(BlogResultEvent.CREATE_FRIEND_SHIP_RESULT, resultHandler);
			addEventListener(BlogResultEvent.DELETE_FRIEND_SHIP_RESULT, resultHandler);
			addEventListener(BlogResultEvent.UPDATE_PROFILE_IMAGE_RESULT, resultHandler);
			addEventListener(BlogResultEvent.UPDATE_PROFILE_RESULT, resultHandler);
			
			// Error
			addEventListener(BlogErrorEvent.CREATE_FAVORITES_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.DELETE_FAVORITES_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.SEND_DIRECT_MESSAGE_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.DELETE_DIRECT_MESSAGE_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.USER_VERIFIER_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.SEARCH_USERS_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.CREATE_FRIEND_SHIP_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.REPLY_COMMENT_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.COMMENT_STATUES_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.DELETE_BLOCKS_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.CREATE_BLOCKS_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.DELETE_STATUS_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.DELETE_FRIEND_SHIP_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.UPDATE_PROFILE_IMAGE_ERROR, errorHandler);
			addEventListener(BlogErrorEvent.UPDATE_PROFILE_ERROR, errorHandler);
		}
		
		
		private function resultHandler(e:BlogResultEvent):void
		{
			switch(e.type)
			{
				case BlogResultEvent.CREATE_FAVORITES_RESULT:
					out('收藏成功');
					break;
				case BlogResultEvent.DELETE_FAVORITES_RESULT:
					out('取消收藏成功');
					break;
				case BlogResultEvent.SEND_DIRECT_MESSAGE_RESULT:
					out('信件已发送');
					break;
				case BlogResultEvent.DELETE_DIRECT_MESSAGE_RESULT:
					out('信件已删除');
					break;
				case BlogResultEvent.REPLY_COMMENT_RESULT:
					out('回复评论成功');
					break;
				case BlogResultEvent.CREATE_FRIEND_SHIP_RESULT:
					out('关注成功');
					break;
				case BlogResultEvent.DELETE_FRIEND_SHIP_RESULT:
					out('取消关注成功');
					break;
				case BlogResultEvent.UPDATE_PROFILE_IMAGE_RESULT:
					out('图像更新成功');
					break;
				case BlogResultEvent.UPDATE_PROFILE_RESULT:
					out('资料更新成功');
					break;
				case BlogResultEvent.USER_VERIFIER_RESULT:
					break;
				default:
					break;
			}
		}
		
		private function errorHandler(e:BlogErrorEvent):void
		{
			var xml:XML = new XML(e.data);
			switch(e.type)
			{
				case BlogErrorEvent.USER_VERIFIER_ERROR:
					out('登录失败 ：用户名密码错误 或 微博未开通');
					break;
				case BlogErrorEvent.CREATE_FRIEND_SHIP_ERROR:
					if(xml)
					{
						if(xml.error != '40303:Error: already followed')
							out(xml.error);
					}
					else
						out('关注失败');
					break;
				case BlogErrorEvent.DELETE_FRIEND_SHIP_ERROR:
					if(xml)
						out(xml.error);
					else
						out('取消关注失败');
					break;
				case BlogErrorEvent.UPDATE_PROFILE_ERROR:
					if(xml)
						out(xml.error);
					else
						out('更新资料失败');
					break;
				case BlogErrorEvent.UPDATE_PROFILE_IMAGE_ERROR:
					if(xml)
						out(xml.error);
					else
						out('更新图像失败');
					break;
				default :
					out('服务器未响应');
					break;
			}
		}
		
		public function clean():void
		{
//			user = null; // NULL  catch Error when DirectMessage was Loading
			userBlogUser = null;
			friendsBlogUser = null;
			followerBlogUser = null;
		}
		
		
		private function out(msg:String):void
		{
			WindowUtil.toolTip(msg);
		}
		
		
		private var _user:BlogUser;

		/**登录用户的信息*/
		public function get user():BlogUser
		{
			return _user;
		}

		/**
		 * @private
		 */
		public function set user(value:BlogUser):void
		{
			_user = value;
		}

		
		/**未读消息*/
		public var unReadMessage:BlogCount;
		
		
		//----------------------------------------------------------------------------------------------
		//
		//                MainContentMain界面
		//
		//----------------------------------------------------------------------------------------------
		
		public var mainSinceID:Number = 0;
		
		//----------------------------------------------------------------------------------------------
		//
		//                MainContentUser界面
		//
		//----------------------------------------------------------------------------------------------
		private var _userBlogUser:BlogUser;
		
		/**
		 *  MainContentUser 界面查询的用户
		 * */
		public function get userBlogUser():BlogUser
		{
			if(_userBlogUser)
				return _userBlogUser;
			else
				return _user;
		}
		
		/**
		 * @private
		 */
		public function set userBlogUser(value:BlogUser):void
		{
			_userBlogUser = value;
		}
		
		
		//----------------------------------------------------------------------------------------------
		//
		//                MainContentFriends界面
		//
		//----------------------------------------------------------------------------------------------
		private var _friendsBlogUser:BlogUser;
		
		/**
		 * MainContentFriends界面查询的用户
		 * */
		public function get friendsBlogUser():BlogUser
		{
			if(_friendsBlogUser)
				return _friendsBlogUser;
			else 
				return _user;
		}
		
		/**
		 * @private
		 */
		public function set friendsBlogUser(value:BlogUser):void
		{
			_friendsBlogUser = value;
		}
		
		
		//----------------------------------------------------------------------------------------------
		//
		//                MainContentFollower界面
		//
		//----------------------------------------------------------------------------------------------
		private var _followerBlogUser:BlogUser;
		
		public function get followerBlogUser():BlogUser
		{
			if(_followerBlogUser)
				return _followerBlogUser;
			else 
				return _user;
		}
		
		public function set followerBlogUser(value:BlogUser):void
		{
			_followerBlogUser = value;
		}
		
		
		override protected function urlLoaderError(e:IOErrorEvent):void
		{
			WindowUtil.load(false);
			super.urlLoaderError(e);
		}
		
		override protected function urlLoaderResult(e:Event):void
		{
			WindowUtil.load(false);
			super.urlLoaderResult(e);
		}
	}
}